{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#人工智能流程 logitic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [2]]\n",
      "[[1]]\n",
      "[[ 0.00216711]\n",
      " [ 0.00877235]]\n",
      "[[ 0.]\n",
      " [ 0.]]\n",
      "0.2\n",
      "\n",
      "(1, 1)\n",
      "[[ 0.50492779]]\n",
      "**\n",
      "[[ 0.68333984]]\n",
      "\n",
      "[[1]\n",
      " [2]]\n",
      "[[1]]\n",
      "[[ 0.10118155]\n",
      " [ 0.20680123]]\n",
      "[[ 0.09901444]\n",
      " [ 0.09901444]]\n",
      "0.2\n"
     ]
    }
   ],
   "source": [
    "#1. 正向传播\n",
    "import numpy as np\n",
    "\n",
    "class Cell(object):\n",
    "    def __init__(self):\n",
    "        self.X = np.array([[1,],[2,]])\n",
    "        self.Y = ([[1,],])\n",
    "        self.W = np.random.randn(2, 1) * 0.01\n",
    "        self.B = np.zeros([2, 1])\n",
    "        self.r = 0.2\n",
    "    \n",
    "    def line_calu(self, W, X, B):\n",
    "        return np.sum(W * X + B, axis=0).reshape([1, 1])\n",
    "        \n",
    "    def sigmod(self, ent):\n",
    "        return 1 / (np.array([[1, ], ])+np.exp(-ent))\n",
    "    \n",
    "    def derivative_sigmod(self, y_):\n",
    "        return y_*(np.array([[1, ], ])-y_)\n",
    "    \n",
    "    def tanh(self, ent):\n",
    "        return (np.exp(ent)-np.exp(-ent)) / (np.exp(ent)+np.exp(-ent))\n",
    "                    \n",
    "    def derivative_tanh(self, y_):\n",
    "        return np.array([[1, ], ]) - np.square(y_)\n",
    "\n",
    "    def relu(self, ent):\n",
    "        return np.max(0, ent)\n",
    "    \n",
    "    def derivative_relu(self, y_):\n",
    "        if y_>=0:\n",
    "            return 1\n",
    "        else:\n",
    "            return 0\n",
    "    \n",
    "    def leaky_relu(self, ent):\n",
    "        return np.max(np.array([[0.01, ], ]) * ent, ent)\n",
    "    \n",
    "    def derivative_leaky_relu(self, y_):\n",
    "        if y_>=0:\n",
    "            return 1\n",
    "        else:\n",
    "            return 0.01\n",
    "    \n",
    "    def cost_function(self, Y, Y_):\n",
    "        print('**')\n",
    "#         print(Y * np.array([-1]))\n",
    "        print(np.array([-1]) * Y * np.log(Y_) + (np.array([-1]) * Y + 1) * np.log(np.array([-1]) * Y_ + 1))\n",
    "#         return np.sum(-Y * np.log(Y_) + (1-Y) * np.log(1-Y_), axis=0)\n",
    "    \n",
    "    def derivative(self, Y, Y_):\n",
    "        m = np.shape(self.X)[1]\n",
    "        dz = self.r * (Y_-Y)\n",
    "        dw = 1 * dz * self.X / m\n",
    "        db = 1 * np.sum(dz, axis=1, keepdims=True) / m\n",
    "        return dz, dw, db\n",
    "    \n",
    "    \n",
    "    def calu_1(self, W, X, B):\n",
    "        return self.sigmod(self.line_calu(self.W, self.X, self.B))\n",
    "        \n",
    "    def update_para(self, Y, Y_):\n",
    "        dz, dw, db = self.derivative(Y, Y_)\n",
    "        self.W = self.W - dw\n",
    "        self.B = self.B - db\n",
    "        \n",
    "    def run(self):\n",
    "        print(self.X)\n",
    "        print(self.Y)\n",
    "        print(self.W)\n",
    "        print(self.B)\n",
    "        print(self.r)\n",
    "        Y_ = self.calu_1(self.W, self.X, self.B)\n",
    "        print()\n",
    "        print(np.shape(Y_))\n",
    "        print(Y_)\n",
    "        C = self.cost_function(self.Y, Y_)\n",
    "        self.update_para(self.Y, Y_)\n",
    "        print()\n",
    "        print(self.X)\n",
    "        print(self.Y)\n",
    "        print(self.W)\n",
    "        print(self.B)\n",
    "        print(self.r)\n",
    "        \n",
    "        pass\n",
    "    \n",
    "if __name__ == \"__main__\":\n",
    "    mycell = Cell()\n",
    "    mycell.run()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
